package com.wp.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wp.file.common.BaseContext;
import com.wp.file.common.R;
import com.wp.file.pojo.Recycle;
import com.wp.file.pojo.Warehouse;
import com.wp.file.service.RecycleService;
import com.wp.file.service.WarehouseService;
import com.wp.file.utils.FileSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Slf4j
@RequestMapping("/api/warehouse")
@RestController
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private RecycleService recycleService;

    /**
     * 查询当前用户的当前路径的所属文件列表展示
     * @param path 当前文件路径
     * @return
     */
    @GetMapping("/findfilelist")
    public R<List<Warehouse>> findFileList(@RequestParam("path") String path){
        //需要文件路径
        //获取用户的id
        Long currentUserId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Warehouse::getUserId,currentUserId).eq(Warehouse::getPath,path);
        lqw.last("order by sort != '0',update_time asc");
        List<Warehouse> warehouseList = warehouseService.list(lqw);
        return R.success(warehouseList);
    }

    /**
     * 在当前路径下新建文件夹
     * @param warehouse
     * @return
     */
    @PostMapping("/newfolder")
    public R<String> createFolder(@RequestBody Warehouse warehouse){
//        String newName = warehouse.getDisplayName();
//        String newPath = warehouse.getPath();
        warehouse.setSort(0);//设置为文件夹种类
        warehouse.setUserId(BaseContext.getCurrentId());
        warehouseService.save(warehouse);
        //如果重名咋办
        //重名我们之接不让重名把 让前端来完成
        return R.success("创建文件夹成功!");
    }

    /**
     * 文件重命名
     * @param
     * @return
     */
    @PutMapping("/rename")
    public R<String> rename(@RequestBody Warehouse newWarehouse) {
        warehouseService.updateById(newWarehouse);
        return R.success("重命名成功!");
    }




}
